package com.xindong.paking.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindong.paking.dtos.PageResult;
import com.xindong.paking.entity.TbMessage;
import com.xindong.paking.mapper.TbMessageMapper;
import com.xindong.paking.service.TbMessageService;
import com.xindong.paking.utils.Result;
import com.xindong.paking.utils.ResultStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangshengce
 * @since 2023-01-11
 */
@Service
public class TbMessageServiceImpl extends ServiceImpl<TbMessageMapper, TbMessage> implements TbMessageService {

    @Autowired
    private TbMessageMapper tbMessageMapper;

    @Override
    public Result addMessage(TbMessage message) {
        // 发送时间
        message.setSendTime(LocalDateTime.now());
        //信息状态
        message.setStatus(1);
        tbMessageMapper.insert(message);
        return new Result(ResultStatus.SUCCESS);
    }

    @Override
    public Result deleteMessage(Integer id) {
        tbMessageMapper.deleteById(id);
        return Result.success("删除成功", null);
    }


    @Override
    public Result updateMessageStatus(Integer id) {

        TbMessage tbMessage = tbMessageMapper.selectById(id);

        //修改状态
        tbMessage.setStatus(0);
        tbMessageMapper.updateById(tbMessage);
        return Result.success("修改成功", null);
    }

    /**
     * 分页条件查询
     *
     * @param page     当前页
     * @param pageSize 每页大小
     * @return
     */
    @Override
    public Result list(Integer page, Integer pageSize) {
        // 设置分页
        IPage<TbMessage> ipage = new Page<>(page, pageSize);
        // 进行分页查询
        IPage<TbMessage> tbMessageIPage = tbMessageMapper.selectPage(ipage, null);
        // 获取分页后数据
        List<TbMessage> records = tbMessageIPage.getRecords();
        // 分页返回数据
        PageResult result = new PageResult(records, tbMessageIPage.getTotal(), page, pageSize);
        return Result.success("获取数据成功", result);
    }
}
